package com.ruoyi.car.aspect;

import com.ruoyi.car.annotation.AutoFill3;
import com.ruoyi.car.domain.TmdCarList;
import com.ruoyi.car.domain.TmdCarRecord;
import com.ruoyi.car.domain.TmdCarUse;
import com.ruoyi.car.mapper.TmdCarRecordMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.OperationType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.aspectj.weaver.ast.Instanceof;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * ClassName: AutoFillOperate
 * Pacakage: com.ruoyi.car.aspect
 * Description:
 *
 * @Author: 刘松泽
 * @Create 2024/4/28 9:21
 * @Version 1.0
 */
@Aspect
@Component
public class AutoFillOperateAspect {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private TmdCarRecordMapper carRecordMapper;


    /**
     * 切入点
     */
    @Pointcut("execution(* com.ruoyi.car.mapper.*.*(..)) && @annotation(com.ruoyi.car.annotation.AutoFill3)")
    public void autoFillPointCut() {
    }

    /**
     * 前置通知,在通知中进行公共字段的赋值
     */
    @After("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {

        //获得到当前被拦截的方法上的数据库操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFill3 autoFill3 = signature.getMethod().getAnnotation(AutoFill3.class);
        OperationType operationType = autoFill3.value();

        //获取当前被拦截的方法的参数--实体对象
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }
//        Object entity = args[0];
//        TmdCarRecord carRecord = new TmdCarRecord();
//        if (entity instanceof TmdCarList) {
//            carRecord.setCarId(((TmdCarList) entity).getId());
//        } else if (entity instanceof TmdCarUse) {
//            carRecord.setCarId(((TmdCarUse) entity).getCarId());
//        } else {
//            Object[] obj = (Object[]) entity;
//            carRecord.setCarId((Long.valueOf(obj[0].toString())));
//        }
        Object entity = args[0];
        TmdCarRecord carRecord = new TmdCarRecord();
        if (entity instanceof TmdCarList) {
            carRecord.setCarId(((TmdCarList) entity).getId());
        } else if (entity instanceof TmdCarUse) {
            carRecord.setCarId(((TmdCarUse) entity).getCarId());
        } else if (entity instanceof Object[]) {
            Object[] obj = (Object[]) entity;
            if (obj.length > 0 && obj[0] instanceof Long) {
                carRecord.setCarId((Long) obj[0]);
            }
        }



        //准备赋值的数据
        Long userId = ShiroUtils.getSysUser().getUserId();
        SysUser user = userMapper.getUserByUserId(userId);
        String userName = user.getUserName();

        if (operationType == OperationType.INSERT) {
            carRecord.setCreatePersonId(userName);
            carRecord.setOperate(2L);
            carRecord.setCreateTime(new Date());
            carRecordMapper.insertTmdCarRecord(carRecord);
        } else if (operationType == OperationType.UPDATE) {
            carRecord.setCreatePersonId(userName);
            carRecord.setOperate(4L);
            carRecord.setCreateTime(new Date());
            carRecordMapper.insertTmdCarRecord(carRecord);
        } else if (operationType == OperationType.DELETE) {
            carRecord.setCreatePersonId(userName);
            carRecord.setOperate(3L);
            carRecord.setCreateTime(new Date());
            carRecordMapper.insertTmdCarRecord(carRecord);
        } else if (operationType == OperationType.RETURN) {
            carRecord.setCreatePersonId(userName);
            carRecord.setOperate(1L);
            carRecord.setCreateTime(new Date());
            carRecordMapper.insertTmdCarRecord(carRecord);
        } else if (operationType == OperationType.BORROW) {
            carRecord.setCreatePersonId(userName);
            carRecord.setOperate(0L);
            carRecord.setCreateTime(new Date());
            carRecordMapper.insertTmdCarRecord(carRecord);
        }
    }
}
